<template>
  <div class="sidebar">
    <el-menu class="sidebar-el-menu" background-color="#324157" text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened :collapse="collapse" router :default-active="curRoute">
      <template v-for="item in items">
        <template v-if="item.subs">
          <el-submenu :index="item.index" :key="item.index">
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{item.title}}</span>
            </template>
            <template v-for="subItem in item.subs">
              <el-submenu index="1-4" v-if="subItem.subs" :key="subItem.index">
                <template slot="title">
                  <span>{{subItem.title}}</span>
                </template>
                <el-menu-item v-for='(sub) in subItem.subs' :index="sub.index" :key="sub.idex">{{sub.title}}</el-menu-item>
              </el-submenu>
              <el-menu-item :index="subItem.index" v-else :key="subItem.index">{{subItem.title}}</el-menu-item>
            </template>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item :index="item.index" :key="item.index">
            <i :class="item.icon"></i>
            <span slot="title">{{item.title}}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>
<script>
import bus from './bus'
export default {
  data () {
    return {
      collapse: false,
      items: [
        {
          index: 'dashboard',
          icon: 'el-icon-menu',
          title: '系统首页'
        },
        {
          index: 'table',
          icon: 'el-icon-menu',
          title: '基础表格'
        },
        {
          index: 'tabs',
          icon: 'el-icon-menu',
          title: 'tab选项卡'
        },
        {
          index: '3',
          icon: 'el-icon-menu',
          title: '表单相关',
          subs: [
            {
              index: 'form',
              title: '基本表单'
            },
            {
              index: '3-2',
              title: '三级菜单',
              subs: [
                {
                  index: 'editor',
                  title: '富文本编辑器'
                }, {
                  index: 'markdown',
                  title: 'markdown编辑器'
                }
              ]
            }, {
              index: 'upload',
              title: '文件上传'
            }
          ]
        }, {
          index: 'icon',
          icon: 'el-icon-menu',
          title: '自定义图标'
        }, {
          index: 'schart',
          icon: 'el-icon-menu',
          title: 'schart图表'
        },
        {
          index: 'drag',
          icon: 'el-icon-menu',
          title: '拖拽列表'
        },
        {
          index: '7',
          icon: 'el-icon-menu',
          title: '错误处理',
          subs: [
            {
              index: 'permission',
              title: '权限测试'
            },
            {
              index: '404',
              title: '404页面'
            }
          ]
        }
      ]
    }
  },
  computed: {
    curRoute () {
      return this.$route.path.replace('/', '')
    }
  },
  mounted () {
    bus.$on('collapse', msg => {
      this.collapse = msg
    })
  }
}
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}
.sidebar > ul {
  height: 100%;
}
</style>
